/*
 * > /*
 >  * INSANE - Interactive Structural Analysis Environment
 >  *
 >  * Copyright (C) 2003-2004
 >  * Universidade Federal de Minas Gerais
 >  * Escola de Engenharia
 >  * Departamento de Engenharia de Estruturas
 >  *
 >  * Author's email :     insane@dees.ufmg.br
 >  * Author's Website :   http://www.dees.ufmg.br/insane
 >  *
 >  * This program is free software; you can redistribute it and/or
 >  * modify it under the terms of the GNU General Public License
 >  * as published by the Free Software Foundation; either version 2
 >  * of the License, or any later version.
 >  *
 >  * This program is distributed in the hope that it will be useful,
 >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 >  * GNU General Public License for more details.
 >  *
 >  * You should have received a copy of the GNU General Public License
 >  * along with this program; if not, write to the Free Software
 >  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 > 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.command;

import java.util.ArrayList;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.persistence.PostpPersistence.PostpPersistenceAsXML;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since January 2004
 */
public class ForwardStepCommand implements Command {
	
	private InterfacePostp parent;
	private PostpModel postpModel = new PostpModel();
	private StepPostp stepPostp = new StepPostp();
	private ArrayList steps;
	
//	*****************************************************************************
	/** Creates new GridCommand */
	public ForwardStepCommand(JInternalFrame parent) {
		this.parent = (InterfacePostp)parent;
		
	}
	
	/** Creates new GridCommand */
	public ForwardStepCommand() {
		
	}
//	*****************************************************************************
	
	public void execute() {
		
		if(((PostpViewState)this.parent.getView().getViewState()).isDeformedOn()){
			if(((PostpViewState)this.parent.getView().getViewState()).getLoadViewState().isOnMemoryOpt()){
				StepPostp current = ((StepPostp)this.parent.getModel()); 
				int index = this.parent.getPostpModel().getStepList().indexOf(current);
				int cont = (int)this.parent.getDesktop().getCurrentStepField().getValue();
				this.parent.getDesktop().getCurrentStepField().setValue(cont);
				if(index==this.parent.getPostpModel().getStepList().size()-1){
					index = 0;
					cont=1;
					this.parent.getDesktop().getCurrentStepField().setValue(cont);
					this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(index)));
				}else{
					cont+=1;
					this.parent.getDesktop().getCurrentStepField().setValue(cont);
					this.parent.setStep((StepPostp)(this.parent.getPostpModel().getStepList().get(index+1)));
				}
				
			}else if(((PostpViewState)this.parent.getView().getViewState()).getLoadViewState().isOnDiskOpt()){
				steps =((PostpViewState)this.parent.getView().getViewState()).getLoadViewState().getStepLoaded();
				int currentStep = (int)this.parent.getDesktop().getCurrentStepField().getValue();
				if(this.parent.getDesktop().getCurrentStepField().getValue()>=steps.size()){
					currentStep = 1;
					PostpPersistenceAsXML p = new PostpPersistenceAsXML();
					p.fillInsaneFromFile((String)steps.get(currentStep-1));
					stepPostp = new StepPostp();
					stepPostp = p.getStep();
					postpModel = new PostpModel();
					postpModel.addStep(stepPostp);
					parent.setStep((StepPostp)postpModel.getStepList().get(0));
					parent.setPostpModel(postpModel);
					parent.toFront();
					this.parent.getDesktop().getCurrentStepField().setValue(currentStep);
				}else{
					PostpPersistenceAsXML p = new PostpPersistenceAsXML();
					p.fillInsaneFromFile((String)steps.get(currentStep));
					stepPostp = new StepPostp();
					stepPostp = p.getStep();
					postpModel = new PostpModel();
					postpModel.addStep(stepPostp);
					parent.setStep((StepPostp)postpModel.getStepList().get(0));
					parent.setPostpModel(postpModel);
					parent.toFront();
					this.parent.getDesktop().getCurrentStepField().setValue((int)this.parent.getDesktop().getCurrentStepField().getValue()+1);
				}
			}
			this.parent.getView().getDrawingArea().repaint();
		}else{
			String message = "You must set Deformed shape!";
			JOptionPane.showMessageDialog(this.parent.getDesktop(), message, "Error Message", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
//	*****************************************************************************
	
	public void undo() {
	}
	
//	*****************************************************************************
	
	public void redo() {
	}
	public InterfacePostp getParent() {
		return parent;
	}
	public void setParent(InterfacePostp parent) {
		this.parent = parent;
	}
	
//	*****************************************************************************
}
